package com.pls.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.write.WriteException;

import com.pls.data.EmployeeSeatData;
import com.pls.entity.Employee;
import com.pls.entity.EmployeeSeat;
import com.pls.entity.EmployeeSeatModifier;
import com.pls.entity.Locator;
import com.pls.entity.Project;
import com.pls.excelwriter.WriteExcel;

public class MainProgram {

	public void printEmployeeSeat(EmployeeSeat empSeat) {
		System.out.println("Name: " + empSeat.getEmployee().getLastName()
				+ ", " + empSeat.getEmployee().getFirstName());
		System.out.println("Projects: ");

		List<Project> projectList = empSeat.getEmployee().getProjectList();

		if (projectList != null) {
			for (Project project : projectList) {
				System.out.println(project.getProjectCode());
			}
		}

		System.out.println("Seat Number: " + empSeat.getSeatCode());
		System.out.println("Seat Type: "
				+ empSeat.getSeatType().getSeat_type_desc());
		System.out.println("Quadrant: " + empSeat.getQuadrant());
		System.out.println("Local: " + empSeat.getLocalNo());

	}

	public void searchEmployee(BufferedReader reader,

	List<EmployeeSeat> employeeSeats, EmployeeSeat empSeat) {
		System.out.println();
		System.out.println("*********Search Employee*********");
		Employee employee = new Employee();
		ProgramHelper helper = new ProgramHelper();
		int employeeId = 0;
		try {
			employeeId = helper.executeIntParseReader(reader,
					"Enter Employee ID: ",
					"Please Enter a Valid Employee ID: ", 6);
		} catch (IOException e1) {
			System.out.println(e1.getMessage());
		}
		Locator locator = new Locator();

		empSeat = locator.searchEmployeeSeat(employeeId);

		if (empSeat.equals(null)) {
			employee = employee.getEmployeeWithProjects(employeeId);

			if (employee.equals(null)) {
				System.out.println("Employee Does Not Exist");
				return;
			}

			empSeat = new EmployeeSeat();
			empSeat.setEmployee(employee);

			printEmployeeSeat(empSeat);
			return;
		}

		printEmployeeSeat(empSeat);
		employeeSeats = locator.getEmployeeSeatByFloorAndQuadrant(
				empSeat.getQuadrant(), empSeat.getFloor_level());

		int space = Configuration.seatSpace;
		int index = 0;
		int empSpace;
		int seatSpace;
		int localSpace;

		for (int row = 1; row < 6; row++) {
			try {

				StringBuilder sbSeatCode = new StringBuilder(" | ");
				StringBuilder sbEmpId = new StringBuilder(" | ");
				StringBuilder sbLocalNo = new StringBuilder(" | ");
				while (employeeSeats.get(index).getRow() == row) {

					String seatCode = employeeSeats.get(index).getSeatCode();

					String employeeCode = "";

					if (employeeSeats.get(index).getEmployee().getId() == 0) {
						employeeCode = "Not Assigned";

					} else if (employeeSeats.get(index).getEmployee().getId() == empSeat
							.getEmployee().getId()) {
						employeeCode = "*EMP"
								+ employeeSeats.get(index).getEmployee()
										.getId() + "*";
					} else {
						employeeCode = "EMP"
								+ employeeSeats.get(index).getEmployee()
										.getId();
					}

					String localNo = (employeeSeats.get(index).getLocalNo() == null) ? ""
							: employeeSeats.get(index).getLocalNo();

					empSpace = space - employeeCode.length();
					seatSpace = space - seatCode.length();
					localSpace = space - localNo.length();

					// Append Seat Code in String Builder
					sbSeatCode.append(seatCode);
					for (int i = 0; i < seatSpace; i++) {
						sbSeatCode.append(" ");
					}
					sbSeatCode.append("| ");

					// Append Employee ID in String Builder
					sbEmpId.append(employeeCode);
					for (int i = 0; i < empSpace; i++) {
						sbEmpId.append(" ");
					}
					sbEmpId.append("| ");

					// Append Local Number in String Builder
					sbLocalNo.append(localNo);
					for (int i = 0; i < localSpace; i++) {
						sbLocalNo.append(" ");
					}
					sbLocalNo.append("| ");

					index++;

					if (employeeSeats.size() == index) {
						break;
					}
				}

				System.out.println(sbSeatCode.toString());
				System.out.println(sbEmpId.toString());
				System.out.println(sbLocalNo.toString());

				System.out.println();

			} catch (IndexOutOfBoundsException e) {
				break;
			}
		}

		try {
			String answer = helper.executeStringAnswerReader(reader,
					"Export Employee as Text File? (y/n): ",
					"Enter Valid Answer: ");
			if (answer.toLowerCase().equals("y")) {
				exportEmployee(employeeSeats, empSeat);
			}

			answer = helper.executeStringAnswerReader(reader,
					"Export Employee as Excel file? (y/n): ",
					"Enter Valid Answer: ");

			if (answer.toLowerCase().equals("y")) {
				WriteExcel excelWriter = new WriteExcel();
				excelWriter.write(employeeSeats, empSeat);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void exportEmployee(List<EmployeeSeat> quadrant,
			EmployeeSeat employee) {

		File file = new File(Configuration.fileDirectory
				+ employee.getEmployee().getId() + employee.getSeatCode()
				+ ".txt");
		FileWriter writer = null;
		try {
			writer = new FileWriter(file);

			int space = Configuration.seatSpace;
			int empSpace;
			int seatSpace;
			int localSpace;
			int index = 0;
			for (int row = 1; row < 6; row++) {
				StringBuilder sbSeatCode = new StringBuilder(" | ");
				StringBuilder sbEmpId = new StringBuilder(" | ");
				StringBuilder sbLocalNo = new StringBuilder(" | ");
				while (quadrant.get(index).getRow() == row) {

					String seatCode = quadrant.get(index).getSeatCode();
					String employeeCode = "";

					if (quadrant.get(index).getEmployee().getId() == 0) {
						employeeCode = "Not Assigned";

					} else if (quadrant.get(index).getEmployee().getId() == employee
							.getEmployee().getId()) {
						employeeCode = "*EMP"
								+ quadrant.get(index).getEmployee()
										.getId() + "*";
					} else {
						employeeCode = "EMP"
								+ quadrant.get(index).getEmployee()
										.getId();
					}


					String localNo = (quadrant.get(index).getLocalNo() == null) ? ""
							: quadrant.get(index).getLocalNo();

					empSpace = space - employeeCode.length();
					seatSpace = space - seatCode.length();
					localSpace = space - localNo.length();

					// Append Seat Code in String Builder
					sbSeatCode.append(seatCode);
					for (int i = 0; i < seatSpace; i++) {
						sbSeatCode.append(" ");
					}
					sbSeatCode.append("| ");

					// Append Employee ID in String Builder
					sbEmpId.append(employeeCode);
					for (int i = 0; i < empSpace; i++) {
						sbEmpId.append(" ");
					}
					sbEmpId.append("| ");

					// Append Local Number in String Builder
					sbLocalNo.append(localNo);
					for (int i = 0; i < localSpace; i++) {
						sbLocalNo.append(" ");
					}
					sbLocalNo.append("| ");

					index++;

					if (quadrant.size() == index) {
						break;
					}

				}

				writer.write(sbSeatCode.toString() + "\r\n");
				writer.write(sbEmpId.toString() + "\r\n");
				writer.write(sbLocalNo.toString() + "\r\n");
				writer.write("\r\n");

			}
		} catch (IOException e) {
			System.out.println(e);
		} finally {
			try {
				if (writer != null) {
					writer.close();
				}
			} catch (IOException e) {
				System.out.println(e);
			}
		}

		System.out.println("Employee Seat Was Successfully Exported!");
		System.out.println();
	}

	// public void updateEmployeeSeat(BufferedReader reader) {
	// System.out.println("*********Update Employee Seat********");
	// ProgramHelper helper = new ProgramHelper();
	// EmployeeSeatModifier empSeat = new EmployeeSeatModifier();
	//
	// int rowsAffected = 0;
	//
	// int employeeId;
	// String newSeatCode;
	//
	// try {
	// employeeId = helper.executeIntParseReader(reader,
	// "Enter Employee ID: ",
	// "Please Enter a Valid Employee ID: ", 6);
	// newSeatCode = helper.executeStringReader(reader,
	// "Enter Seat Code: ", "Please Ebter Valid Seat Code: ");
	//
	// rowsAffected = empSeat.updateEmployeeSeat(employeeId, newSeatCode);
	//
	// if (rowsAffected > 0) {
	// System.out.println("Successfully Updated Employee Seat");
	// }
	//
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }

	public void updateEmployeeSeat(BufferedReader reader) throws IOException {
		System.out.println();
		System.out.println("*********Update Employee*********");

		ProgramHelper helper = new ProgramHelper();
		int employeeId = 0;
		String newSeatCode;

		employeeId = helper.executeIntParseReader(reader,
				"Enter Employee ID: ", "Please Enter a Valid Employee ID: ", 6);
		newSeatCode = helper.executeStringReader(reader, "Enter Seat Code: ",
				"Please Ebter Valid Seat Code: ");

		EmployeeSeatData empSeatData = new EmployeeSeatData();

		EmployeeSeatData empSeatData2 = new EmployeeSeatData();

		Boolean isSeatCodeExist = empSeatData
				.validateSeatCodeExist(newSeatCode);

		List<EmployeeSeat> empSeatList = empSeatData2
				.selectEmployeeSeatInformation(newSeatCode, isSeatCodeExist);

		if (empSeatList == null) {
			return;
		}
		// Get EmployeeSeatDetails
		for (EmployeeSeat employeeSeat : empSeatList) {

			EmployeeSeatData empSeatData3 = new EmployeeSeatData();

			EmployeeSeat empSeat2 = new EmployeeSeat();
			empSeat2 = empSeatData3.getEmployeeSeatDetails(employeeSeat
					.getEmployee().getId());
			int tableEmployeeId = empSeat2.getEmployee().getId();

			int empSeat1 = 0;

			if (isSeatCodeExist == true && tableEmployeeId == 0) {
				// update
				EmployeeSeatModifier empSeatMod = new EmployeeSeatModifier();

				empSeat1 = empSeatMod.updateEmployeeSeat(employeeId,
						newSeatCode, empSeat2.getSeatID());

			}

			if (isSeatCodeExist == true && tableEmployeeId > 0) {
				// insert
				EmployeeSeatModifier empSeatMod3 = new EmployeeSeatModifier();

				String locCode = newSeatCode.substring(0, 3);

				String floorLevel = "";

				if (locCode.equals("PTC")) {
					floorLevel = newSeatCode.substring(4, 6);
				}

				if (locCode.equals("PIC")) {
					floorLevel = newSeatCode.substring(3, 5);
				}

				int empIntSeat2 = empSeatMod3.insertEmployeeSeat(employeeId,
						floorLevel, empSeat2, isSeatCodeExist);

			}

		}
	}

	public void deleteEmployee(BufferedReader reader) throws IOException {
		System.out.println();
		System.out.println("*********Delete Employee*********");
		ProgramHelper helper = new ProgramHelper();
		int delEmployeeId = 0;
		String delSeatCode;

		delEmployeeId = helper.executeIntParseReader(reader,
				"Enter Employee ID: ", "Please Enter a Valid Employee ID: ", 6);
		delSeatCode = helper.executeStringReader(reader, "Enter Seat Code: ",
				"Please Ebter Valid Seat Code: ");

		EmployeeSeatData empDelSeatData = new EmployeeSeatData();

		EmployeeSeatData empDelSeatData2 = new EmployeeSeatData();

		Boolean isDelSeatCodeExist = empDelSeatData
				.validateSeatCodeExist(delSeatCode);

		List<EmployeeSeat> empDelSeatList = empDelSeatData2
				.selectEmployeeSeatInformation(delSeatCode, isDelSeatCodeExist);

		// Get EmployeeSeatDetails
		if (empDelSeatList.equals(null)) {
			System.out.println("EMployee is not in the seat plan.");
		}
		if (!empDelSeatList.equals(null)) {
			for (EmployeeSeat employeeDelSeat : empDelSeatList) {

				EmployeeSeatData empDelSeatData3 = new EmployeeSeatData();

				EmployeeSeat empDelSeat2 = new EmployeeSeat();
				empDelSeat2 = empDelSeatData3
						.getEmployeeSeatDetails(employeeDelSeat.getEmployee()
								.getId());
				int tableDelEmployeeId = empDelSeat2.getEmployee().getId();

				int empDelSeat1 = 0;

				if (isDelSeatCodeExist == true && tableDelEmployeeId == 0) {
					System.out.println("No occupant in the seatcode provided.");
				}

				if (isDelSeatCodeExist == true && tableDelEmployeeId > 0) {
					// insert
					EmployeeSeatModifier empDelSeatMod3 = new EmployeeSeatModifier();

					int empDelIntSeat2 = empDelSeatMod3.deleteEmployeeSeat(
							delEmployeeId, delSeatCode);
				}

			}

		}
	}

	public void executeCommand(int command, BufferedReader reader)
			throws IOException {
		switch (command) {
		case 1:
			List<EmployeeSeat> employeeSeats = new ArrayList<EmployeeSeat>();
			EmployeeSeat empSeat = new EmployeeSeat();
			searchEmployee(reader, employeeSeats, empSeat);

			break;
		case 2:
			updateEmployeeSeat(reader);

			break;
		case 3:

			deleteEmployee(reader);

			break;
		case 4:
			System.out.println("Program was Terminated! ");
			return;
		default:
			break;
		}
	}
}
